package com.kaibes.platform.file.client.api;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.platform.file.client.db.FileServer;
import com.kaibes.platform.file.client.db.FileServerService;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;

@RestController
@RequestMapping("/file/servers")
public class FileServerApi extends ResponseApi<FileServer, FileServerService> {

	@ApiLink("创建服务器信息")
    @PostMapping
    public ResponseEntity<List<Number>> post(@RequestAttribute Integer userId, @RequestBody FileServer fileServer) {
		String host = fileServer.getHost();
		if (host.endsWith("/")) {
			fileServer.setHost(host.substring(0, host.length()-1));
		}
        return super.post(fileServer, "创建服务器信息成功", "创建服务器信息失败");
    }
	
	@DeleteMapping("/{id:\\d+}")
    @ApiLink("删除服务器信息，根据id")
    public ResponseEntity<Object> delete(@PathVariable @Description("映射ID") Integer id) {
        return super.deleteById(id, "删除服务器信息成功", "删除服务器信息失败");
    }
	
	@ApiLink("修改服务器信息")
	@PutMapping("/{id:\\d+}")
    public ResponseEntity<Object> put(@RequestAttribute Integer userId, @PathVariable @Description("映射ID") Integer id, @RequestBody FileServer fileServer) {
        return super.putById(fileServer, id, "修改服务器信息成功", "修改服务器信息失败");
    }
	
	@GetMapping("/{id:\\d+}")
    @ApiLink("获取服务器信息，根据id")
    public ResponseEntity<FileServer> get(@PathVariable @Description("映射ID") Integer id) {
        return super.getById(id, "获取服务器信息成功", "获取服务器信息失败");
    }
	
}
